<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.38" />
    <meta name="theme" content="VuePress Theme Hope" />
    <meta property="og:url" content="https://javaguide.cn/tools/docker/docker-in-action.html"><meta property="og:site_name" content="JavaGuide"><meta property="og:title" content="Docker从入门到上手干事"><meta property="og:type" content="article"><meta property="og:updated_time" content="2022-03-03T12:39:23.000Z"><meta property="og:locale" content="zh-CN"><meta property="article:tag" content="Docker"><meta property="article:modified_time" content="2022-03-03T12:39:23.000Z"><script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?5dd2e8c97962d57b7b8fea1737c01743";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script><link rel="stylesheet" href="//at.alicdn.com/t/font_2922463_99aa80ii7cf.css"><title>Docker从入门到上手干事 | JavaGuide</title><meta name="description" content="Java学习&&面试指南">
    <style>
      :root {
        --bg-color: #fff;
      }

      html[data-theme="dark"] {
        --bg-color: #1d2025;
      }

      html,
      body {
        background-color: var(--bg-color);
      }
    </style>
    <script>
      const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
      const systemDarkMode =
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches;

      if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
        document.querySelector("html").setAttribute("data-theme", "dark");
      }
    </script>
    <link rel="stylesheet" href="/assets/style.aa943f56.css">
    <link rel="modulepreload" href="/assets/app.93341f6d.js"><link rel="modulepreload" href="/assets/docker-in-action.html.4af8813a.js"><link rel="modulepreload" href="/assets/docker-in-action.html.4d4c352e.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper.21dcd24c.js">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="skip-link sr-only">Skip to content</a><!--]--><div class="theme-container has-toc sidebar-open"><!--[--><header class="navbar"><button class="toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><a href="/" class="home-link"><img class="logo" src="/logo.png" alt="JavaGuide"><!----><span class="site-name hide-in-pad">JavaGuide</span><!--[--><!----><!--]--></a><nav class="nav-links" style=""><div class="nav-item hide-in-mobile"><a href="/home.html" class="nav-link" arialabel="面试指南"><i class="icon iconfont icon-java"></i>面试指南<!----></a></div><div class="nav-item hide-in-mobile"><a href="/zhuanlan/" class="nav-link" arialabel="优质专栏"><i class="icon iconfont icon-recommend"></i>优质专栏<!----></a></div><div class="nav-item hide-in-mobile"><a href="/open-source-project/" class="nav-link" arialabel="项目精选"><i class="icon iconfont icon-github"></i>项目精选<!----></a></div><div class="nav-item hide-in-mobile"><a href="/books/" class="nav-link" arialabel="书籍精选"><i class="icon iconfont icon-book"></i>书籍精选<!----></a></div><div class="nav-item hide-in-mobile"><a href="https://snailclimb.gitee.io/javaguide/#/" rel="noopener noreferrer" target="_blank" arialabel="旧版链接" class="nav-link"><i class="icon iconfont icon-java"></i>旧版链接<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a></div><div class="nav-item hide-in-mobile"><a href="https://javaguide.cn/feed.json" rel="noopener noreferrer" target="_blank" arialabel="RSS订阅" class="nav-link"><i class="icon iconfont icon-rss"></i>RSS订阅<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a></div><div class="nav-item hide-in-mobile"><a href="/about-the-author/" class="nav-link" arialabel="关于作者"><i class="icon iconfont icon-zuozhe"></i>关于作者<!----></a></div></nav><div class="nav-actions-wrapper"><!--[--><!----><!--]--><div class="nav-item"><!----></div><div class="nav-item"><a class="repo-link" href="https://github.com/Snailclimb/JavaGuide" target="_blank" rel="noopener noreferrer"><svg xmlns="http://www.w3.org/2000/svg" class="icon github-icon" viewbox="0 0 1024 1024" arialabelledby="github" style="width:1.25rem;height:1.25rem;vertical-align:middle;"><title id="github" lang="en">github icon</title><g fill="currentColor"><path d="M511.957 21.333C241.024 21.333 21.333 240.981 21.333 512c0 216.832 140.544 400.725 335.574 465.664 24.49 4.395 32.256-10.07 32.256-23.083 0-11.69.256-44.245 0-85.205-136.448 29.61-164.736-64.64-164.736-64.64-22.315-56.704-54.4-71.765-54.4-71.765-44.587-30.464 3.285-29.824 3.285-29.824 49.195 3.413 75.179 50.517 75.179 50.517 43.776 75.008 114.816 53.333 142.762 40.79 4.523-31.66 17.152-53.377 31.19-65.537-108.971-12.458-223.488-54.485-223.488-242.602 0-53.547 19.114-97.323 50.517-131.67-5.035-12.33-21.93-62.293 4.779-129.834 0 0 41.258-13.184 134.912 50.346a469.803 469.803 0 0 1 122.88-16.554c41.642.213 83.626 5.632 122.88 16.554 93.653-63.488 134.784-50.346 134.784-50.346 26.752 67.541 9.898 117.504 4.864 129.834 31.402 34.347 50.474 78.123 50.474 131.67 0 188.586-114.73 230.016-224.042 242.09 17.578 15.232 33.578 44.672 33.578 90.454v135.85c0 13.142 7.936 27.606 32.854 22.87C862.25 912.597 1002.667 728.747 1002.667 512c0-271.019-219.648-490.667-490.71-490.667z"></path></g></svg></a></div><div class="nav-item hide-in-mobile"><button id="appearance-switch"><svg xmlns="http://www.w3.org/2000/svg" class="icon auto-icon" viewbox="0 0 1024 1024" arialabelledby="auto" style="display:block;"><title id="auto" lang="en">auto icon</title><g fill="currentColor"><path d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z"></path></g></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewbox="0 0 1024 1024" arialabelledby="dark" style="display:none;"><title id="dark" lang="en">dark icon</title><g fill="currentColor"><path d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z"></path></g></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" viewbox="0 0 1024 1024" arialabelledby="light" style="display:none;"><title id="light" lang="en">light icon</title><g fill="currentColor"><path d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z"></path></g></svg></button></div><form class="search-box" role="search"><input type="search" placeholder="搜索" autocomplete="off" spellcheck="false" value><!----></form><button class="toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span class="button-container"><span class="button-top"></span><span class="button-middle"></span><span class="button-bottom"></span></span></button><!--[--><!----><!--]--></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow left"></span></div><aside class="sidebar"><!--[--><!----><!--]--><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-mianshi"></i><span class="title">面试准备</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-java"></i><span class="title">Java</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-computer"></i><span class="title">计算机基础</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-database"></i><span class="title">数据库</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable active"><i class="icon iconfont icon-Tools"></i><span class="title">开发工具</span><span class="arrow down"></span></button><ul class="sidebar-links"><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-git"></i><span class="title">Git</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable active"><i class="icon iconfont icon-docker1"></i><span class="title">Docker</span><span class="arrow down"></span></button><ul class="sidebar-links"><li><!--[--><a href="/tools/docker/docker-intro.html" class="nav-link sidebar-link sidebar-page" arialabel="Docker 基本概念解读"><!---->Docker 基本概念解读<!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li><li><!--[--><a aria-current="page" href="/tools/docker/docker-in-action.html" class="router-link-active router-link-exact-active nav-link active sidebar-link sidebar-page active" arialabel="Docker从入门到上手干事"><!---->Docker从入门到上手干事<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker介绍" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker介绍"><!---->Docker介绍<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#什么是-docker" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="什么是 Docker？"><!---->什么是 Docker？<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#为什么要用-docker" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="为什么要用 Docker?"><!---->为什么要用 Docker?<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-的安装" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 的安装"><!---->Docker 的安装<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#windows" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Windows"><!---->Windows<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#mac" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Mac"><!---->Mac<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#linux" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Linux"><!---->Linux<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-中的几个概念" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 中的几个概念"><!---->Docker 中的几个概念<!----></a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#镜像" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="镜像"><!---->镜像<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#容器" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="容器"><!---->容器<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#仓库" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="仓库"><!---->仓库<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#总结" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="总结"><!---->总结<!----></a><ul class="sidebar-sub-headers"></ul></li></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-初体验" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 初体验"><!---->Docker 初体验<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-镜像指令" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 镜像指令"><!---->Docker 镜像指令<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-容器指令" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 容器指令"><!---->Docker 容器指令<!----></a><ul class="sidebar-sub-headers"></ul></li><li class="sidebar-sub-header"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-数据卷" class="router-link-active router-link-exact-active nav-link sidebar-link heading" arialabel="Docker 数据卷"><!---->Docker 数据卷<!----></a><ul class="sidebar-sub-headers"></ul></li></ul><!--]--></li></ul></section><!--]--></li><li><!--[--><a href="https://gitee.com/SnailClimb/awesome-idea-tutorial" rel="noopener noreferrer" target="_blank" arialabel="IDEA" class="nav-link sidebar-link sidebar-page"><i class="icon iconfont icon-intellijidea"></i>IDEA<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a><ul class="sidebar-sub-headers"></ul><!--]--></li></ul></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-xitongsheji"></i><span class="title">系统设计</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-distributed-network"></i><span class="title">分布式</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-et-performance"></i><span class="title">高性能</span><span class="arrow right"></span></button><!----></section><!--]--></li><li><!--[--><section class="sidebar-group"><button class="sidebar-heading clickable"><i class="icon iconfont icon-CalendarAvailability-1"></i><span class="title">高可用</span><span class="arrow right"></span></button><!----></section><!--]--></li></ul><!--[--><!----><!--]--></aside><!--[--><main class="page" id="main-content"><!----><nav class="breadcrumb disable"></nav><div class="page-title"><h1><!---->Docker从入门到上手干事</h1><div class="article-info"><span class="author-info" arialabel="作者🖊" isoriginal="false" pageview="false" color="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon author-icon" viewbox="0 0 1024 1024" arialabelledby="author"><title id="author" lang="en">author icon</title><g fill="currentColor"><path d="M649.6 633.6c86.4-48 147.2-144 147.2-249.6 0-160-128-288-288-288s-288 128-288 288c0 108.8 57.6 201.6 147.2 249.6-121.6 48-214.4 153.6-240 288-3.2 9.6 0 19.2 6.4 25.6 3.2 9.6 12.8 12.8 22.4 12.8h704c9.6 0 19.2-3.2 25.6-12.8 6.4-6.4 9.6-16 6.4-25.6-25.6-134.4-121.6-240-243.2-288z"></path></g></svg><span><a class="author-item" href="https://javaguide.cn/" target="_blank" rel="noopener noreferrer">Guide</a></span><span property="author" content="Guide"></span></span><span class="category-info" arialabel="分类🌈" isoriginal="false" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon category-icon" viewbox="0 0 1024 1024" arialabelledby="category"><title id="category" lang="en">category icon</title><g fill="currentColor"><path d="M148.41 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H148.41c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.311-40.31zM147.556 553.478H429.73c22.263 0 40.311 18.048 40.311 40.31v282.176c0 22.263-18.048 40.312-40.31 40.312H147.555c-22.263 0-40.311-18.049-40.311-40.312V593.79c0-22.263 18.048-40.311 40.31-40.311zM593.927 106.992h282.176c22.263 0 40.31 18.048 40.31 40.31V429.48c0 22.263-18.047 40.31-40.31 40.31H593.927c-22.263 0-40.311-18.047-40.311-40.31V147.302c0-22.263 18.048-40.31 40.31-40.31zM730.22 920.502H623.926c-40.925 0-74.22-33.388-74.22-74.425V623.992c0-41.038 33.387-74.424 74.425-74.424h222.085c41.038 0 74.424 33.226 74.424 74.067v114.233c0 10.244-8.304 18.548-18.547 18.548s-18.548-8.304-18.548-18.548V623.635c0-20.388-16.746-36.974-37.33-36.974H624.13c-20.585 0-37.331 16.747-37.331 37.33v222.086c0 20.585 16.654 37.331 37.126 37.331H730.22c10.243 0 18.547 8.304 18.547 18.547 0 10.244-8.304 18.547-18.547 18.547z"></path></g></svg><ul class="categories-wrapper"><li class="category clickable" role="navigation">开发工具</li><meta property="articleSection" content="开发工具"></ul></span><span arialabel="标签🏷" isoriginal="false" pageview="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon tag-icon" viewbox="0 0 1024 1024" arialabelledby="tag"><title id="tag" lang="en">tag icon</title><g fill="currentColor"><path d="M939.902 458.563L910.17 144.567c-1.507-16.272-14.465-29.13-30.737-30.737L565.438 84.098h-.402c-3.215 0-5.726 1.005-7.634 2.913l-470.39 470.39a10.004 10.004 0 000 14.164l365.423 365.424c1.909 1.908 4.42 2.913 7.132 2.913s5.223-1.005 7.132-2.913l470.39-470.39c2.01-2.11 3.014-5.023 2.813-8.036zm-240.067-72.121c-35.458 0-64.286-28.828-64.286-64.286s28.828-64.285 64.286-64.285 64.286 28.828 64.286 64.285-28.829 64.286-64.286 64.286z"></path></g></svg><ul class="tags-wrapper"><li class="tag clickable" role="navigation">Docker</li></ul><meta property="keywords" content="Docker"></span><span class="date-info" arialabel="写作日期📅" isoriginal="false" pageview="false" color="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewbox="0 0 1024 1024" arialabelledby="calendar"><title id="calendar" lang="en">calendar icon</title><g fill="currentColor"><path d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z"></path></g></svg><span>2022年1月12日</span><meta property="datePublished" content="2022-01-12T01:32:00.000Z"></span><!----><span class="words-info" arialabel="字数🔠" isoriginal="false" pageview="false" color="false"><svg xmlns="http://www.w3.org/2000/svg" class="icon word-icon" viewbox="0 0 1024 1024" arialabelledby="word"><title id="word" lang="en">word icon</title><g fill="currentColor"><path d="M518.217 432.64V73.143A73.143 73.143 0 01603.43 1.097a512 512 0 01419.474 419.474 73.143 73.143 0 01-72.046 85.212H591.36a73.143 73.143 0 01-73.143-73.143z"></path><path d="M493.714 566.857h340.297a73.143 73.143 0 0173.143 85.577A457.143 457.143 0 11371.566 117.76a73.143 73.143 0 0185.577 73.143v339.383a36.571 36.571 0 0036.571 36.571z"></path></g></svg><span>约 5008 字</span><meta property="wordCount" content="5008"></span></div><hr></div><div class="toc-place-holder"><aside id="toc-list"><div class="toc-header">此页内容</div><div class="toc-wrapper"><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker介绍" class="router-link-active router-link-exact-active toc-link level2">Docker介绍</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#什么是-docker" class="router-link-active router-link-exact-active toc-link level3">什么是 Docker？</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#为什么要用-docker" class="router-link-active router-link-exact-active toc-link level3">为什么要用 Docker?</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-的安装" class="router-link-active router-link-exact-active toc-link level2">Docker 的安装</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#windows" class="router-link-active router-link-exact-active toc-link level3">Windows</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#mac" class="router-link-active router-link-exact-active toc-link level3">Mac</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#linux" class="router-link-active router-link-exact-active toc-link level3">Linux</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-中的几个概念" class="router-link-active router-link-exact-active toc-link level2">Docker 中的几个概念</a></li><ul class="toc-list"><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#镜像" class="router-link-active router-link-exact-active toc-link level3">镜像</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#容器" class="router-link-active router-link-exact-active toc-link level3">容器</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#仓库" class="router-link-active router-link-exact-active toc-link level3">仓库</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#总结" class="router-link-active router-link-exact-active toc-link level3">总结</a></li><!----><!--]--></ul><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-初体验" class="router-link-active router-link-exact-active toc-link level2">Docker 初体验</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-镜像指令" class="router-link-active router-link-exact-active toc-link level2">Docker 镜像指令</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-容器指令" class="router-link-active router-link-exact-active toc-link level2">Docker 容器指令</a></li><!----><!--]--><!--[--><li class="toc-item"><a aria-current="page" href="/tools/docker/docker-in-action.html#docker-数据卷" class="router-link-active router-link-exact-active toc-link level2">Docker 数据卷</a></li><!----><!--]--></ul></div></aside></div><!----><div class="theme-hope-content"><!--[--><h2 id="docker介绍" tabindex="-1"><a class="header-anchor" href="#docker介绍" aria-hidden="true">#</a> Docker介绍</h2><h3 id="什么是-docker" tabindex="-1"><a class="header-anchor" href="#什么是-docker" aria-hidden="true">#</a> 什么是 Docker？</h3><p>说实话关于 Docker 是什么并不太好说，下面我通过四点向你说明 Docker 到底是个什么东西。</p><ul><li>Docker 是世界领先的软件容器平台，基于 <strong>Go 语言</strong> 进行开发实现。</li><li>Docker 能够自动执行重复性任务，例如搭建和配置开发环境，从而解放开发人员。</li><li>用户可以方便地创建和使用容器，把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改，就像管理普通的代码一样。</li><li>Docker 可以<strong>对进程进行封装隔离，属于操作系统层面的虚拟化技术。</strong> 由于隔离的进程独立于宿主和其它的隔离的进程，因此也称其为容器。</li></ul><p>官网地址：https://www.docker.com/ 。</p><p><img src="https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/container.png" alt="认识容器" loading="lazy"></p><h3 id="为什么要用-docker" tabindex="-1"><a class="header-anchor" href="#为什么要用-docker" aria-hidden="true">#</a> 为什么要用 Docker?</h3><p>Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中，然后发布到任何流行的 Linux 机器上，也可以实现虚拟化。</p><p>容器是完全使用沙箱机制，相互之间不会有任何接口（类似 iPhone 的 app），更重要的是容器性能开销极低。</p><p>传统的开发流程中，我们的项目通常需要使用 MySQL、Redis、FastDFS 等等环境，这些环境都是需要我们手动去进行下载并配置的，安装配置流程极其复杂，而且不同系统下的操作也不一样。</p><p>Docker 的出现完美地解决了这一问题，我们可以在容器中安装 MySQL、Redis 等软件环境，使得应用和环境架构分开，它的优势在于：</p><ol><li>一致的运行环境，能够更轻松地迁移</li><li>对进程进行封装隔离，容器与容器之间互不影响，更高效地利用系统资源</li><li>可以通过镜像复制多个一致的容器</li></ol><p>另外，<a href="https://yeasy.gitbook.io/docker_practice/introduction/why" target="_blank" rel="noopener noreferrer">《Docker 从入门到实践》<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a> 这本开源书籍中也已经给出了使用 Docker 的原因。</p><p><img src="https://img-blog.csdnimg.cn/20210412220015698.png" alt="" loading="lazy"></p><h2 id="docker-的安装" tabindex="-1"><a class="header-anchor" href="#docker-的安装" aria-hidden="true">#</a> Docker 的安装</h2><h3 id="windows" tabindex="-1"><a class="header-anchor" href="#windows" aria-hidden="true">#</a> Windows</h3><p>接下来对 Docker 进行安装，以 Windows 系统为例，访问 Docker 的官网：</p><p><img src="https://oscimg.oschina.net/oscnet/up-4e3146984adaee0067bdc5e9b1d757bb479.png" alt="" loading="lazy"></p><p>然后点击<code>Get Started</code>：</p><p><img src="https://oscimg.oschina.net/oscnet/up-96adfbfebe3e59097c8ba25e55f68ba7908.png" alt="" loading="lazy"></p><p>在此处点击<code>Download for Windows</code>即可进行下载。</p><p>如果你的电脑是<code>Windows 10 64位专业版</code>的操作系统，则在安装 Docker 之前需要开启一下<code>Hyper-V</code>，开启方式如下。打开控制面板，选择程序：</p><p><img src="https://oscimg.oschina.net/oscnet/up-73ce678240826de0f49225250a970b4d205.png" alt="" loading="lazy"></p><p>点击<code>启用或关闭Windows功能</code>：</p><p><img src="https://oscimg.oschina.net/oscnet/up-9c7a96c332e56b9506325a1f1fdb608a659.png" alt="" loading="lazy"></p><p>勾选上<code>Hyper-V</code>，点击确定即可：</p><p><img src="https://oscimg.oschina.net/oscnet/up-aad4a58c5e917f7185908d6320d7fb06861.png" alt="" loading="lazy"></p><p>完成更改后需要重启一下计算机。</p><p>开启了<code>Hyper-V</code>后，我们就可以对 Docker 进行安装了，打开安装程序后，等待片刻点击<code>Ok</code>即可：</p><p><img src="https://oscimg.oschina.net/oscnet/up-62ac3c9184bdc21387755294613ff5054c6.png" alt="" loading="lazy"></p><p>安装完成后，我们仍然需要重启计算机，重启后，若提示如下内容：</p><p><img src="https://oscimg.oschina.net/oscnet/up-3585c7d6a4632134ed925493a7d43e14a43.png" alt="" loading="lazy"></p><p>它的意思是询问我们是否使用 WSL2，这是基于 Windows 的一个 Linux 子系统，这里我们取消即可，它就会使用我们之前勾选的<code>Hyper-V</code>虚拟机。</p><p>因为是图形界面的操作，这里就不介绍 Docker Desktop 的具体用法了。</p><h3 id="mac" tabindex="-1"><a class="header-anchor" href="#mac" aria-hidden="true">#</a> Mac</h3><p>直接使用 Homebrew 安装即可</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>brew <span class="token function">install</span> --cask <span class="token function">docker</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="linux" tabindex="-1"><a class="header-anchor" href="#linux" aria-hidden="true">#</a> Linux</h3><p>下面来看看 Linux 中如何安装 Docker，这里以 CentOS7 为例。</p><p>在测试或开发环境中，Docker 官方为了简化安装流程，提供了一套便捷的安装脚本，执行这个脚本后就会自动地将一切准备工作做好，并且把 Docker 的稳定版本安装在系统中。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">curl</span> -fsSL get.docker.com -o get-docker.sh
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">sh</span> get-docker.sh --mirror Aliyun
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>安装完成后直接启动服务：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>systemctl start <span class="token function">docker</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>推荐设置开机自启，执行指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>systemctl <span class="token builtin class-name">enable</span> <span class="token function">docker</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><h2 id="docker-中的几个概念" tabindex="-1"><a class="header-anchor" href="#docker-中的几个概念" aria-hidden="true">#</a> Docker 中的几个概念</h2><p>在正式学习 Docker 之前，我们需要了解 Docker 中的几个核心概念：</p><h3 id="镜像" tabindex="-1"><a class="header-anchor" href="#镜像" aria-hidden="true">#</a> 镜像</h3><p>镜像就是一个只读的模板，镜像可以用来创建 Docker 容器，一个镜像可以创建多个容器</p><h3 id="容器" tabindex="-1"><a class="header-anchor" href="#容器" aria-hidden="true">#</a> 容器</h3><p>容器是用镜像创建的运行实例，Docker 利用容器独立运行一个或一组应用。它可以被启动、开始、停止、删除，每个容器都是相互隔离的、保证安全的平台。 可以把容器看作是一个简易的 Linux 环境和运行在其中的应用程序。容器的定义和镜像几乎一模一样，也是一堆层的统一视角，唯一区别在于容器的最上面那一层是可读可写的</p><h3 id="仓库" tabindex="-1"><a class="header-anchor" href="#仓库" aria-hidden="true">#</a> 仓库</h3><p>仓库是集中存放镜像文件的场所。仓库和仓库注册服务器是有区别的，仓库注册服务器上往往存放着多个仓库，每个仓库中又包含了多个镜像，每个镜像有不同的标签。 仓库分为公开仓库和私有仓库两种形式，最大的公开仓库是 DockerHub，存放了数量庞大的镜像供用户下载，国内的公开仓库有阿里云、网易云等</p><h3 id="总结" tabindex="-1"><a class="header-anchor" href="#总结" aria-hidden="true">#</a> 总结</h3><p>通俗点说，一个镜像就代表一个软件；而基于某个镜像运行就是生成一个程序实例，这个程序实例就是容器；而仓库是用来存储 Docker 中所有镜像的。</p><p>其中仓库又分为远程仓库和本地仓库，和 Maven 类似，倘若每次都从远程下载依赖，则会大大降低效率，为此，Maven 的策略是第一次访问依赖时，将其下载到本地仓库，第二次、第三次使用时直接用本地仓库的依赖即可，Docker 的远程仓库和本地仓库的作用也是类似的。</p><h2 id="docker-初体验" tabindex="-1"><a class="header-anchor" href="#docker-初体验" aria-hidden="true">#</a> Docker 初体验</h2><p>下面我们来对 Docker 进行一个初步的使用，这里以下载一个 MySQL 的镜像为例<code>(在CentOS7下进行)</code>。</p><p>和 GitHub 一样，Docker 也提供了一个 DockerHub 用于查询各种镜像的地址和安装教程，为此，我们先访问 DockerHub：<a href="https://hub.docker.com/" target="_blank" rel="noopener noreferrer">https://hub.docker.com/<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span></a></p><p><img src="https://oscimg.oschina.net/oscnet/up-37d083cc92fe36aad829e975646b9d27fa0.png" alt="" loading="lazy"></p><p>在左上角的搜索框中输入<code>MySQL</code>并回车：</p><p><img src="https://oscimg.oschina.net/oscnet/up-ced37002391a059754def9b3a6c2aa4e342.png" alt="" loading="lazy"></p><p>可以看到相关 MySQL 的镜像非常多，若右上角有<code>OFFICIAL IMAGE</code>标识，则说明是官方镜像，所以我们点击第一个 MySQL 镜像：</p><p><img src="https://oscimg.oschina.net/oscnet/up-48ba3fdc99c93a96e18b929195ca8e93c6c.png" alt="" loading="lazy"></p><p>右边提供了下载 MySQL 镜像的指令为<code>docker pull MySQL</code>，但该指令始终会下载 MySQL 镜像的最新版本。</p><p>若是想下载指定版本的镜像，则点击下面的<code>View Available Tags</code>：</p><p><img src="https://oscimg.oschina.net/oscnet/up-ed601649275c6cfe65bbe422b463c263a64.png" alt="" loading="lazy"></p><p>这里就可以看到各种版本的镜像，右边有下载的指令，所以若是想下载 5.7.32 版本的 MySQL 镜像，则执行：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> pull MySQL:5.7.32
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>然而下载镜像的过程是非常慢的，所以我们需要配置一下镜像源加速下载，访问<code>阿里云</code>官网：</p><p><img src="https://oscimg.oschina.net/oscnet/up-0a46effd262d3db1b613a0db597efa31f34.png" alt="" loading="lazy"></p><p>点击控制台：</p><p><img src="https://oscimg.oschina.net/oscnet/up-60f198e0106be6b43044969d2900272504f.png" alt="" loading="lazy"></p><p>然后点击左上角的菜单，在弹窗的窗口中，将鼠标悬停在产品与服务上，并在右侧搜索容器镜像服务，最后点击容器镜像服务：</p><p><img src="https://oscimg.oschina.net/oscnet/up-2f6706a979b405dab01bc44a29bb6b26fc4.png" alt="" loading="lazy"></p><p>点击左侧的镜像加速器，并依次执行右侧的配置指令即可。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">sudo</span> <span class="token function">mkdir</span> -p /etc/docker
<span class="token function">sudo</span> <span class="token function">tee</span> /etc/docker/daemon.json <span class="token operator">&lt;&lt;-</span><span class="token string">&#39;EOF&#39;
{
  &quot;registry-mirrors&quot;: [&quot;https://679xpnpz.mirror.aliyuncs.com&quot;]
}
EOF</span>
<span class="token function">sudo</span> systemctl daemon-reload
<span class="token function">sudo</span> systemctl restart <span class="token function">docker</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="docker-镜像指令" tabindex="-1"><a class="header-anchor" href="#docker-镜像指令" aria-hidden="true">#</a> Docker 镜像指令</h2><p>Docker 需要频繁地操作相关的镜像，所以我们先来了解一下 Docker 中的镜像指令。</p><p>若想查看 Docker 中当前拥有哪些镜像，则可以使用 <code>docker images</code> 命令。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker images</span>
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
MySQL         <span class="token number">5.7</span>.32    f07dfa83b528   <span class="token number">11</span> days ago     448MB
tomcat        latest    feba8d001e3f   <span class="token number">2</span> weeks ago     649MB
nginx         latest    ae2feff98a0c   <span class="token number">2</span> weeks ago     133MB
hello-world   latest    bf756fb1ae65   <span class="token number">12</span> months ago   <span class="token number">13</span>.3kB
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>其中<code>REPOSITORY</code>为镜像名，<code>TAG</code>为版本标志，<code>IMAGE ID</code>为镜像 id(唯一的)，<code>CREATED</code>为创建时间，注意这个时间并不是我们将镜像下载到 Docker 中的时间，而是镜像创建者创建的时间，<code>SIZE</code>为镜像大小。</p><p>该指令能够查询指定镜像名：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> image MySQL
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>若如此做，则会查询出 Docker 中的所有 MySQL 镜像：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker images MySQL</span>
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
MySQL        <span class="token number">5.6</span>       0ebb5600241d   <span class="token number">11</span> days ago     302MB
MySQL        <span class="token number">5.7</span>.32    f07dfa83b528   <span class="token number">11</span> days ago     448MB
MySQL        <span class="token number">5.5</span>       d404d78aa797   <span class="token number">20</span> months ago   205MB
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>该指令还能够携带<code>-q</code>参数：<code>docker images -q</code> ， <code>-q</code>表示仅显示镜像的 id：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker images -q</span>
0ebb5600241d
f07dfa83b528
feba8d001e3f
d404d78aa797
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>若是要下载镜像，则使用：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> pull MySQL:5.7
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><code>docker pull</code>是固定的，后面写上需要下载的镜像名及版本标志；若是不写版本标志，而是直接执行<code>docker pull MySQL</code>，则会下载镜像的最新版本。</p><p>一般在下载镜像前我们需要搜索一下镜像有哪些版本才能对指定版本进行下载，使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> search MySQL
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="https://oscimg.oschina.net/oscnet/up-559083ae80e7501e86e95fbbad25b6d571a.png" alt="" loading="lazy"></p><p>不过该指令只能查看 MySQL 相关的镜像信息，而不能知道有哪些版本，若想知道版本，则只能这样查询：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> search MySQL:5.5
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="https://oscimg.oschina.net/oscnet/up-68394e25f652964bb042571151c5e0fd2e9.png" alt="" loading="lazy"></p><p>若是查询的版本不存在，则结果为空：</p><p><img src="https://oscimg.oschina.net/oscnet/up-abfdd51b9ad2ced3711268369f52b077b12.png" alt="" loading="lazy"></p><p>删除镜像使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> image <span class="token function">rm</span> MySQL:5.5
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>若是不指定版本，则默认删除的也是最新版本。</p><p>还可以通过指定镜像 id 进行删除：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> image <span class="token function">rm</span> bf756fb1ae65
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>然而此时报错了：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker image rm bf756fb1ae65</span>
Error response from daemon: conflict: unable to delete bf756fb1ae65 <span class="token punctuation">(</span>must be forced<span class="token punctuation">)</span> - image is being used by stopped container d5b6c177c151
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这是因为要删除的<code>hello-world</code>镜像正在运行中，所以无法删除镜像，此时需要强制执行删除：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> image <span class="token function">rm</span> -f bf756fb1ae65
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该指令会将镜像和通过该镜像执行的容器全部删除，谨慎使用。</p><p>Docker 还提供了删除镜像的简化版本：<code>docker rmi 镜像名:版本标志</code> 。</p><p>此时我们即可借助<code>rmi</code>和<code>-q</code>进行一些联合操作，比如现在想删除所有的 MySQL 镜像，那么你需要查询出 MySQL 镜像的 id，并根据这些 id 一个一个地执行<code>docker rmi</code>进行删除，但是现在，我们可以这样：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> rmi -f <span class="token variable"><span class="token variable">$(</span><span class="token function">docker</span> images MySQL -q<span class="token variable">)</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>首先通过<code>docker images MySQL -q</code>查询出 MySQL 的所有镜像 id，<code>-q</code>表示仅查询 id，并将这些 id 作为参数传递给<code>docker rmi -f</code>指令，这样所有的 MySQL 镜像就都被删除了。</p><h2 id="docker-容器指令" tabindex="-1"><a class="header-anchor" href="#docker-容器指令" aria-hidden="true">#</a> Docker 容器指令</h2><p>掌握了镜像的相关指令之后，我们需要了解一下容器的指令，容器是基于镜像的。</p><p>若需要通过镜像运行一个容器，则使用：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>当然了，运行的前提是你拥有这个镜像，所以先下载镜像：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> pull tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>下载完成后就可以运行了，运行后查看一下当前运行的容器：<code>docker ps</code> 。</p><p><img src="https://oscimg.oschina.net/oscnet/up-bd48e20ef07b7c91ad16f92821a3dbca5b5.png" alt="" loading="lazy"></p><p>其中<code>CONTAINER_ID</code>为容器的 id，<code>IMAGE</code>为镜像名，<code>COMMAND</code>为容器内执行的命令，<code>CREATED</code>为容器的创建时间，<code>STATUS</code>为容器的状态，<code>PORTS</code>为容器内服务监听的端口，<code>NAMES</code>为容器的名称。</p><p>通过该方式运行的 tomcat 是不能直接被外部访问的，因为容器具有隔离性，若是想直接通过 8080 端口访问容器内部的 tomcat，则需要对宿主机端口与容器内的端口进行映射：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -p <span class="token number">8080</span>:8080 tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>解释一下这两个端口的作用(<code>8080:8080</code>)，第一个 8080 为宿主机端口，第二个 8080 为容器内的端口，外部访问 8080 端口就会通过映射访问容器内的 8080 端口。</p><p>此时外部就可以访问 Tomcat 了：</p><p><img src="https://oscimg.oschina.net/oscnet/up-16d9ff4d29094681f51424ea8d0ee4fd73e.png" alt="" loading="lazy"></p><p>若是这样进行映射：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -p <span class="token number">8088</span>:8080 tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>则外部需访问 8088 端口才能访问 tomcat，需要注意的是，每次运行的容器都是相互独立的，所以同时运行多个 tomcat 容器并不会产生端口的冲突。</p><p>容器还能够以后台的方式运行，这样就不会占用终端：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -d -p <span class="token number">8080</span>:8080 tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>启动容器时默认会给容器一个名称，但这个名称其实是可以设置的，使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -d -p <span class="token number">8080</span>:8080 --name tomcat01 tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>此时的容器名称即为 tomcat01，容器名称必须是唯一的。</p><p>再来引申一下<code>docker ps</code>中的几个指令参数，比如<code>-a</code>：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">ps</span> -a
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该参数会将运行和非运行的容器全部列举出来：</p><p><img src="https://oscimg.oschina.net/oscnet/up-16d9ff4d29094681f51424ea8d0ee4fd73e.png" alt="" loading="lazy"></p><p><code>-q</code>参数将只查询正在运行的容器 id：<code>docker ps -q</code> 。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker ps -q</span>
f3aac8ee94a3
074bf575249b
1d557472a708
4421848ba294
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>若是组合使用，则查询运行和非运行的所有容器 id：<code>docker ps -qa</code> 。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker ps -aq</span>
f3aac8ee94a3
7f7b0e80c841
074bf575249b
a1e830bddc4c
1d557472a708
4421848ba294
b0440c0a219a
c2f5d78c5d1a
5831d1bab2a6
d5b6c177c151
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>接下来是容器的停止、重启指令，因为非常简单，就不过多介绍了。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> start c2f5d78c5d1a
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>通过该指令能够将已经停止运行的容器运行起来，可以通过容器的 id 启动，也可以通过容器的名称启动。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> restart c2f5d78c5d1a
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该指令能够重启指定的容器。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> stop c2f5d78c5d1a
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该指令能够停止指定的容器。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">kill</span> c2f5d78c5d1a
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该指令能够直接杀死指定的容器。</p><p>以上指令都能够通过容器的 id 和容器名称两种方式配合使用。</p><hr><p>当容器被停止之后，容器虽然不再运行了，但仍然是存在的，若是想删除它，则使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">rm</span> d5b6c177c151
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>需要注意的是容器的 id 无需全部写出来，只需唯一标识即可。</p><p>若是想删除正在运行的容器，则需要添加<code>-f</code>参数强制删除：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">rm</span> -f d5b6c177c151
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>若是想删除所有容器，则可以使用组合指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">rm</span> -f <span class="token variable"><span class="token variable">$(</span><span class="token function">docker</span> <span class="token function">ps</span> -qa<span class="token variable">)</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>先通过<code>docker ps -qa</code>查询出所有容器的 id，然后通过<code>docker rm -f</code>进行删除。</p><hr><p>当容器以后台的方式运行时，我们无法知晓容器的运行状态，若此时需要查看容器的运行日志，则使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> logs 289cc00dc5ed
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>这样的方式显示的日志并不是实时的，若是想实时显示，需要使用<code>-f</code>参数：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> logs -f 289cc00dc5ed
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>通过<code>-t</code>参数还能够显示日志的时间戳，通常与<code>-f</code>参数联合使用：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> logs -ft 289cc00dc5ed
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><hr><p>查看容器内运行了哪些进程，可以使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">top</span> 289cc00dc5ed
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="https://oscimg.oschina.net/oscnet/up-7ec71a682712e56e90490f55c32cf660fd3.png" alt="" loading="lazy"></p><p>若是想与容器进行交互，则使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token builtin class-name">exec</span> -it 289cc00dc5ed <span class="token function">bash</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="https://oscimg.oschina.net/oscnet/up-fd17796322f833685ca8ead592d38581898.png" alt="" loading="lazy"></p><p>此时终端将会进入容器内部，执行的指令都将在容器中生效，在容器内只能执行一些比较简单的指令，如：ls、cd 等，若是想退出容器终端，重新回到 CentOS 中，则执行<code>exit</code>即可。</p><p>现在我们已经能够进入容器终端执行相关操作了，那么该如何向 tomcat 容器中部署一个项目呢？</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">cp</span> ./test.html 289cc00dc5ed:/usr/local/tomcat/webapps
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>通过<code>docker cp</code>指令能够将文件从 CentOS 复制到容器中，<code>./test.html</code>为 CentOS 中的资源路径，<code>289cc00dc5ed</code>为容器 id，<code>/usr/local/tomcat/webapps</code>为容器的资源路径，此时<code>test.html</code>文件将会被复制到该路径下。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@izrcf5u3j3q8xaz ~<span class="token punctuation">]</span><span class="token comment"># docker exec -it 289cc00dc5ed bash</span>
root@289cc00dc5ed:/usr/local/tomcat<span class="token comment"># cd webapps</span>
root@289cc00dc5ed:/usr/local/tomcat/webapps<span class="token comment"># ls</span>
test.html
root@289cc00dc5ed:/usr/local/tomcat/webapps<span class="token comment">#</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>若是想将容器内的文件复制到 CentOS 中，则反过来写即可：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> <span class="token function">cp</span> 289cc00dc5ed:/usr/local/tomcat/webapps/test.html ./
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>所以现在若是想要部署项目，则先将项目上传到 CentOS，然后将项目从 CentOS 复制到容器内，此时启动容器即可。</p><hr><p>虽然使用 Docker 启动软件环境非常简单，但同时也面临着一个问题，我们无法知晓容器内部具体的细节，比如监听的端口、绑定的 ip 地址等等，好在这些 Docker 都帮我们想到了，只需使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> inspect 923c969b0d91
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p><img src="https://oscimg.oschina.net/oscnet/up-fca74d4350cdfebfc2b06101e1cab411619.png" alt="" loading="lazy"></p><h2 id="docker-数据卷" tabindex="-1"><a class="header-anchor" href="#docker-数据卷" aria-hidden="true">#</a> Docker 数据卷</h2><p>学习了容器的相关指令之后，我们来了解一下 Docker 中的数据卷，它能够实现宿主机与容器之间的文件共享，它的好处在于我们对宿主机的文件进行修改将直接影响容器，而无需再将宿主机的文件再复制到容器中。</p><p>现在若是想将宿主机中<code>/opt/apps</code>目录与容器中<code>webapps</code>目录做一个数据卷，则应该这样编写指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -d -p <span class="token number">8080</span>:8080 --name tomcat01 -v /opt/apps:/usr/local/tomcat/webapps tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>然而此时访问 tomcat 会发现无法访问：</p><p><img src="https://oscimg.oschina.net/oscnet/up-8fa1b23f6ea2567b5938370e7d7f636533f.png" alt="" loading="lazy"></p><p>这就说明我们的数据卷设置成功了，Docker 会将容器内的<code>webapps</code>目录与<code>/opt/apps</code>目录进行同步，而此时<code>/opt/apps</code>目录是空的，导致<code>webapps</code>目录也会变成空目录，所以就访问不到了。</p><p>此时我们只需向<code>/opt/apps</code>目录下添加文件，就会使得<code>webapps</code>目录也会拥有相同的文件，达到文件共享，测试一下：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@centos-7 opt<span class="token punctuation">]</span><span class="token comment"># cd apps/</span>
<span class="token punctuation">[</span>root@centos-7 apps<span class="token punctuation">]</span><span class="token comment"># vim test.html</span>
<span class="token punctuation">[</span>root@centos-7 apps<span class="token punctuation">]</span><span class="token comment"># ls</span>
test.html
<span class="token punctuation">[</span>root@centos-7 apps<span class="token punctuation">]</span><span class="token comment"># cat test.html</span>
<span class="token operator">&lt;</span>h<span class="token operator"><span class="token file-descriptor important">1</span>&gt;</span>This is a <span class="token builtin class-name">test</span> html<span class="token operator">!</span><span class="token operator">&lt;</span>/h<span class="token operator"><span class="token file-descriptor important">1</span>&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>在<code>/opt/apps</code>目录下创建了一个 <code>test.html</code> 文件，那么容器内的<code>webapps</code>目录是否会有该文件呢？进入容器的终端：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@centos-7 apps<span class="token punctuation">]</span><span class="token comment"># docker exec -it tomcat01 bash</span>
root@115155c08687:/usr/local/tomcat<span class="token comment"># cd webapps/</span>
root@115155c08687:/usr/local/tomcat/webapps<span class="token comment"># ls</span>
test.html
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>容器内确实已经有了该文件，那接下来我们编写一个简单的 Web 应用：</p><div class="language-java ext-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloServlet</span> <span class="token keyword">extends</span> <span class="token class-name">HttpServlet</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        resp<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello World!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doPost</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> req<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> resp<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
        <span class="token function">doGet</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span>resp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>这是一个非常简单的 Servlet，我们将其打包上传到<code>/opt/apps</code>中，那么容器内肯定就会同步到该文件，此时进行访问：</p><p><img src="https://oscimg.oschina.net/oscnet/up-712716a8c8c444ba3a77ade8ff27e7c6cf5.png" alt="" loading="lazy"></p><p>这种方式设置的数据卷称为自定义数据卷，因为数据卷的目录是由我们自己设置的，Docker 还为我们提供了另外一种设置数据卷的方式：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> run -d -p <span class="token number">8080</span>:8080 --name tomcat01 -v aa:/usr/local/tomcat/webapps tomcat:8.0-jre8
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>此时的<code>aa</code>并不是数据卷的目录，而是数据卷的别名，Docker 会为我们自动创建一个名为<code>aa</code>的数据卷，并且会将容器内<code>webapps</code>目录下的所有内容复制到数据卷中，该数据卷的位置在<code>/var/lib/docker/volumes</code>目录下：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@centos-7 volumes<span class="token punctuation">]</span><span class="token comment"># pwd</span>
/var/lib/docker/volumes
<span class="token punctuation">[</span>root@centos-7 volumes<span class="token punctuation">]</span><span class="token comment"># cd aa/</span>
<span class="token punctuation">[</span>root@centos-7 aa<span class="token punctuation">]</span><span class="token comment"># ls</span>
_data
<span class="token punctuation">[</span>root@centos-7 aa<span class="token punctuation">]</span><span class="token comment"># cd _data/</span>
<span class="token punctuation">[</span>root@centos-7 _data<span class="token punctuation">]</span><span class="token comment"># ls</span>
docs  examples  host-manager  manager  ROOT
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>此时我们只需修改该目录的内容就能能够影响到容器。</p><hr><p>最后再介绍几个容器和镜像相关的指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> commit -m <span class="token string">&quot;描述信息&quot;</span> -a <span class="token string">&quot;镜像作者&quot;</span> tomcat01 my_tomcat:1.0
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>该指令能够将容器打包成一个镜像，此时查询镜像：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@centos-7 _data<span class="token punctuation">]</span><span class="token comment"># docker images</span>
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my_tomcat           <span class="token number">1.0</span>                 79ab047fade5        <span class="token number">2</span> seconds ago       463MB
tomcat              <span class="token number">8</span>                   a041be4a5ba5        <span class="token number">2</span> weeks ago         533MB
MySQL               latest              db2b37ec6181        <span class="token number">2</span> months ago        545MB
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>若是想将镜像备份出来，则可以使用指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> save my_tomcat:1.0 -o my-tomcat-1.0.tar
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token punctuation">[</span>root@centos-7 ~<span class="token punctuation">]</span><span class="token comment"># docker save my_tomcat:1.0 -o my-tomcat-1.0.tar</span>
<span class="token punctuation">[</span>root@centos-7 ~<span class="token punctuation">]</span><span class="token comment"># ls</span>
anaconda-ks.cfg  initial-setup-ks.cfg  公共  视频  文档  音乐
get-docker.sh    my-tomcat-1.0.tar     模板  图片  下载  桌面
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>若是拥有<code>.tar</code>格式的镜像，该如何将其加载到 Docker 中呢？执行指令：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">docker</span> load -i my-tomcat-1.0.tar
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br></div></div><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>root@centos-7 ~<span class="token punctuation">]</span><span class="token comment"># docker load -i my-tomcat-1.0.tar</span>
b28ef0b6fef8: Loading layer <span class="token punctuation">[</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">&gt;</span><span class="token punctuation">]</span>  <span class="token number">105</span>.5MB/105.5MB
0b703c74a09c: Loading layer <span class="token punctuation">[</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">==</span><span class="token operator">&gt;</span><span class="token punctuation">]</span>  <span class="token number">23</span>.99MB/23.99MB
<span class="token punctuation">..</span><span class="token punctuation">..</span><span class="token punctuation">..</span>
Loaded image: my_tomcat:1.0
<span class="token punctuation">[</span>root@centos-7 ~<span class="token punctuation">]</span><span class="token comment"># docker images</span>
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my_tomcat           <span class="token number">1.0</span>                 79ab047fade5        <span class="token number">7</span> minutes ago       463MB
</code></pre><div class="line-numbers" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><!--]--></div><!----><footer class="page-meta"><div class="meta-item edit-link"><a href="https://github.com/Snailclimb/JavaGuide/edit/main/docs/tools/docker/docker-in-action.md" rel="noopener noreferrer" target="_blank" arialabel="编辑此页" class="nav-link label"><!--[--><svg xmlns="http://www.w3.org/2000/svg" class="icon edit-icon" viewbox="0 0 1024 1024" arialabelledby="edit"><title id="edit" lang="en">edit icon</title><g fill="currentColor"><path d="M430.818 653.65a60.46 60.46 0 0 1-50.96-93.281l71.69-114.012 7.773-10.365L816.038 80.138A60.46 60.46 0 0 1 859.225 62a60.46 60.46 0 0 1 43.186 18.138l43.186 43.186a60.46 60.46 0 0 1 0 86.373L588.879 565.55l-8.637 8.637-117.466 68.234a60.46 60.46 0 0 1-31.958 11.229z"></path><path d="M728.802 962H252.891A190.883 190.883 0 0 1 62.008 771.98V296.934a190.883 190.883 0 0 1 190.883-192.61h267.754a60.46 60.46 0 0 1 0 120.92H252.891a69.962 69.962 0 0 0-69.098 69.099V771.98a69.962 69.962 0 0 0 69.098 69.098h475.911A69.962 69.962 0 0 0 797.9 771.98V503.363a60.46 60.46 0 1 1 120.922 0V771.98A190.883 190.883 0 0 1 728.802 962z"></path></g></svg><!--]-->编辑此页<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a></div><div class="meta-item update-time"><span class="label">上次编辑于: </span><span class="info">2022/3/3 20:39:23</span></div><div class="meta-item contributors"><span class="label">贡献者: </span><!--[--><!--[--><span class="contributor" title="email: koushuangbwcx@163.com">guide</span><!--]--><!--]--></div></footer><nav class="page-nav"><a href="/tools/docker/docker-intro.html" class="nav-link prev" arialabel="Docker 基本概念解读"><div class="hint"><span class="arrow left"></span>上一页</div><div class="link"><!---->Docker 基本概念解读</div></a><!----></nav><!----><!----></main><!--]--><footer class="footer-wrapper"><div class="footer"><a href="https://beian.miit.gov.cn/" target="_blank">鄂ICP备2020015769号-1</a></div><div class="copyright">Copyright © 2022 Guide</div></footer></div><!--]--><!----><!--]--></div>
    <script type="module" src="/assets/app.93341f6d.js" defer></script>
  </body>
</html>
